package com.exmaple.lemlin.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.exmaple.lemlin.R;
import com.exmaple.lemlin.app.APPIM;
import com.exmaple.lemlin.entity.ChatMessage;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ChatWindowsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {

    private static int CHAT_SEND = 0;
    private static int CHAT_RECEIVE = 1;

    private Context mContext;
    private List<ChatMessage> messages;

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public ChatWindowsAdapter(Context mContext, List<ChatMessage> messages) {
        this.mContext = mContext;
        this.messages = messages;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        if (viewType == CHAT_SEND) {
            View inflate = LayoutInflater.from(mContext).inflate(R.layout.recycler_item_chat_send, viewGroup, (false));
            return new ChatSessionSendViewHolder(inflate);
        }
        if (viewType == CHAT_RECEIVE) {
            View inflate = LayoutInflater.from(mContext).inflate(R.layout.recycler_item_chat_receive, viewGroup, (false));
            return new ChatSessionReceiveViewHolder(inflate);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if (viewHolder instanceof ChatSessionSendViewHolder) {
            ChatSessionSendViewHolder holder = (ChatSessionSendViewHolder) viewHolder;
            holder.tvSend.setText(messages.get(i).getMsgText());
            holder.itemView.setTag(i);
        }
        if (viewHolder instanceof ChatSessionReceiveViewHolder) {
            ChatSessionReceiveViewHolder holder = (ChatSessionReceiveViewHolder) viewHolder;
            holder.tvReceive.setText(messages.get(i).getMsgText());
            holder.itemView.setTag(i);
        }
    }

    @Override
    public int getItemCount() {
        return messages.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (messages.get(position).getFrom()
                .equals(APPIM.getLoginUser().asBareJid().getLocalpartOrNull().intern()))
            return CHAT_SEND;
        return CHAT_RECEIVE;
    }

    /**
     * 加载数据
     */
    public void addItem(ChatMessage message) {
        this.messages.add(getItemCount(), message);
        notifyItemChanged(getItemCount());
    }

    @Override
    public void onClick(View v) {
        if (onItemClickListener != null)
            onItemClickListener.onItemClick(v, (int) v.getTag());
    }

    public interface OnItemClickListener {

        void onItemClick(View view, int position);
    }


    class ChatSessionSendViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_send)
        TextView tvSend;

        ChatSessionSendViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind((this), itemView);
        }
    }

    class ChatSessionReceiveViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_receive)
        TextView tvReceive;

        ChatSessionReceiveViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind((this), itemView);
        }
    }

}
